package com.github_dididipapa.mall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.github_dididipapa.mall.product.entity.ProductAttrValueEntity;
import com.github_dididipapa.mall.product.service.ProductAttrValueService;
import com.github_dididipapa.mall.product.vo.AttrVo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.github_dididipapa.mall.product.service.AttrService;
import com.github_dididipapa.common.utils.PageUtils;
import com.github_dididipapa.common.utils.R;



/**
 * 商品属性
 *
 * @author dididipapa
 * @email not_found@404.com
 * @date 2025-04-26 18:51:11
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("product/attr")
public class AttrController {
    private final AttrService attrService;
    private final ProductAttrValueService productAttrValueService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrVo attr = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateByAttrVo(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }


    @GetMapping("/base/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrService.getBaseAttrPage(params, catelogId);
        return R.ok().put("page", page);
    }


    @GetMapping("/sale/list/{catelogId}")
    public R getAttrByCategoryId(@RequestParam Map<String, Object> params,
                                 @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrService.getByCategoryId(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * @description: 规格修改
     * @author: dididi_papa
     * @date: 2025/6/26 下午3:40
     * @param: [spuId]
     * @return: com.github_dididipapa.common.utils.R
     **/
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId){
        return R.ok().put("data", attrService.getBaseAttrListForSpu(spuId));
    }

    /**
     * @description:
     * @author: dididi_papa
     * @date: 2025/6/29 下午3:35
     * @param: [spuId, entities]
     * @return: com.github_dididipapa.common.utils.R
     **/
    @PostMapping("/update/{spuId}")
    public R updateAttr(@PathVariable("spuId") Long spuId,
                        @RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateBatchBySpuId(spuId, entities);
        return R.ok();
    }
}
